package com.liuy.util.mail;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;


/**
 * 读取相应的配置文件的内容，通过这些内容生成新的对应对象
 * 
 * 
 * */
public class ReadFileNewObj {
	
	/** 
     * 获得邮件会话相关属性   
	 * @throws UnsupportedEncodingException 
     */    
   public static MailSenderInfo getMailSenderInfoProperties() throws UnsupportedEncodingException{    
	   
	Configuration config = null;
	try {
		config = ( Configuration )new PropertiesConfiguration("usermail.properties");
	} catch (ConfigurationException e) {
		e.printStackTrace();
	}
	
	/**
	 * 读取存储邮件内容的文件的目录位置
	 * 
	 * 	"."+File.separator+"bin"+File.separator+"filePath.properties"
	 * 									 是 filePath.properties的存放路径
	 * */
	File file = new File("."+File.separator+"bin"+File.separator+"filePath.properties");
	BufferedReader reader = null;
	String tempString = null;
	String comString = null;
	try {
		//System.out.println("以行为单位读取文件内容，一次读一整行：");
		reader = new BufferedReader(new FileReader(file));
		
		int line = 1;
		//一次读入一行，直到读入null为文件结束
		while ((tempString = reader.readLine()) != null) {
			//显示行号
			//System.out.println("line " + line + ": " + tempString);
			comString = tempString;
			line++;
		}
		reader.close();
	}catch (IOException e) {
		e.printStackTrace();
	}finally {
		if (reader != null) {
			try {
				reader.close();
			} catch (IOException e1) {
			}
		}
	}	
	//System.out.println(comString);
	String [] filepath = (comString.trim()).split("=");
	//System.out.println(filepath[0]);
	//System.out.println(filepath[1]);
	
	
	/**
	 * 配置邮件的相关参数
	 * 
	 * */
	if( filepath == null){
		return null;     
	}else{
		if(config == null ){
			return null;     
		}else{ 
			
			
			MailSenderInfo mailInfo = new MailSenderInfo(); 		
			mailInfo.setMailServerHost(config.getString("mailServerHost").trim()); 		
			mailInfo.setMailServerPort(config.getString("mailServerPort").trim());  
			if ( (config.getString("validate").trim()).equals("true")){
				mailInfo.setValidate(true);    
			}else{
				mailInfo.setValidate(false);    
			}		
			mailInfo.setUserName(config.getString("mailUserName").trim());    
			mailInfo.setPassword(config.getString("mailPassword").trim());//您的邮箱密码    
			mailInfo.setFromAddress(config.getString("fromAddress").trim());    
			mailInfo.setToAddress(config.getString("toAddress").trim());    
			
			//设置邮件内容和标题
			//mailInfo.setContent("邮件内容  如 http://www.google.com.hk 中国达人秀 是中国最好看的节目==");			
			String mailContent = "" ;
			if("".equals(filepath[0])||filepath[0]==null){				
			}else{
				//filepath[0];
				mailContent = readFileByLines(filepath[0]) ;
				
			}
			if("".equals(filepath[1])||filepath[1]==null){				
			}else{
				mailContent =mailContent+"\n"+readFileByLines(filepath[1]);				
			}
			mailInfo.setSubject(config.getString("mailSubject").trim());
			mailInfo.setContent(mailContent);
			//mailContent = mailContent.replaceAll("\r\n|\r|\n|\n\r", "<br>");
			System.out.println("邮件的内容 \n"+mailContent);
			
			
			return mailInfo;	     
	     }    
	}
	
   }    
   
   
   
   
   /**
    * 以行为单位读取文件，常用于读面向行的格式化文件
    * 返回文件的内容
    */
   public static String readFileByLines(String fileName) {
       File file = new File(fileName);
       String fileContents  = "";
       BufferedReader reader = null;
       try {
           //System.out.println("以行为单位读取文件内容，一次读一整行：");
           reader = new BufferedReader(new FileReader(file));
           String tempString = null;
           //int line = 1;
           // 一次读入一行，直到读入null为文件结束
           while ((tempString = reader.readLine()) != null) {
               // 显示行号
               //System.out.println("line " + line + ": " + tempString);        	   
               fileContents=fileContents+"\n"+tempString;
               //line++;
           }
           reader.close();
       } catch (IOException e) {
           e.printStackTrace();
       } finally {
           if (reader != null) {
               try {
                   reader.close();
               } catch (IOException e1) {
               }
           }
       }
       return fileContents ;
   }
   


}
